<?php
namespace Admin\Controller;
use Think\Controller;
class UsersController extends AdminController {
	// 用户列表
    public function index(){
    	$this->assign('users',M('users')->select());
    	$this->display();
    }
    // 添加用户
    public function create(){
    	if(IS_POST){
    		$data['username'] = I('username');
    		$data['password'] = md5(I('password'));
    		$data['email']	= I('email');
    		$data['nicename'] = I('nicename');
    		$data['description'] = I('description');
    		if(I('status_code')=='on'){
    			$data['status'] = 'active';
    		}else $data['status'] = 'forbidden';
    		$data['last_login'] = time();
    		$data['created_at'] = time();
    		$data['updated_at'] = time();

    		if(M('users')->add($data)>0){
    			$this->success('添加成功','/admin/users/index');
    		}else{
    			$this->error('添加失败','/admin/users/index');
    		}
    	}else{
    		$this->display();
    	}
    }
    // 修改用户
    public function edit($id){
    	if(IS_POST){
    		$data['username'] = I('username');
    		$data['email']	= I('email');
    		$data['nicename'] = I('nicename');
    		$data['description'] = I('description');
    		if(I('status_code')=='on'){
    			$data['status'] = 'active';
    		}else $data['status'] = 'forbidden';
    		$data['last_login'] = time();
    		$data['created_at'] = time();
    		$data['updated_at'] = time();
    		if(I('password')!=''){
    			$data['password'] = md5(I('password'));
    		}
    		if(M('users')->where('id=$d',$id)->save($data)>0){
    			$this->success('修改成功','/admin/users/index');
    		}else{
    			$this->error('修改失败','/admin/users/index');
    		}
    	}else{
    		$this->assign('user',M('users')->where('id=%d',$id)->find());
    		$this->display();
    	}
    }

}